package com.jimikongjian.service;

import com.jimikongjian.dao.IUnreadComment;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.UnreadComment;
import com.jimikongjian.service.base.IArticleService;
import com.jimikongjian.service.base.IUnreadCommentService;
import com.jimikongjian.service.base.IUserService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/15.
 */
@Service("unreadCommentService")
public class UnreadCommentService implements IUnreadCommentService {


    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iUnreadCommentImpl")
    private IUnreadComment iUnreadCommentImpl;


    @Autowired
    @Qualifier("userService")
    private IUserService userService;


    @Autowired
    @Qualifier("articleService")
    private IArticleService articleService;


    /**
     * 创建未读评论
     *
     * @param unreadComment
     * @return
     */
    public ResponseMessage createUnreadComment(UnreadComment unreadComment) {
        if (unreadComment.getUserId() > 0
                && unreadComment.getArticleId() > 0
                && unreadComment.getArticleTitle() != ""){
            if (!hasUnreadComment(unreadComment.getUserId(), unreadComment.getArticleId())){
                if (userService.hasUser(unreadComment.getUserId())){
                    if (articleService.hasArticle(unreadComment.getArticleId())){

                        iUnreadCommentImpl.insertUnreadComment(unreadComment);
                        return responseMessageService.makeMessage(Message.SUCCESS);
                    }
                    return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_USER);
            }
            return responseMessageService.makeMessage(Message.HAS_UNREAD_COMMENT);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过用户的id号查询未读信息
     *
     * @param userId
     * @return
     */
    public ResponseMessage selectUnreadCommentByUserId(int userId, int start, int length) {
        if (userId > 0){
            if (userService.hasUser(userId)){
                List<UnreadComment> unreadCommentList = iUnreadCommentImpl.getUnreadCommentListByUserId(userId, start, length);
                return responseMessageService.makeMessage(unreadCommentList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询数据库中是否含有该未读评论信息
     *
     * @param userId
     * @param articleId
     * @return
     */
    public boolean hasUnreadComment(int userId, int articleId) {
        if (userService.hasUser(userId)
                && articleService.hasArticle(articleId)){
            List<UnreadComment> unreadCommentList = iUnreadCommentImpl.hasUnreadComment(userId, articleId);
            if (unreadCommentList.size() > 0){
                return true;
            }
        }
        return false;
    }

    /**
     * 更新未读评论的条数
     *
     * @param id
     */
    public ResponseMessage updateUnreadCommentById(int id) {
        if (id > 0){
            iUnreadCommentImpl.updateUnreadCommentById(id, 0);
            return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 更新未读评论的条数
     *
     * @param articleId
     */
    public void updateUnreadCommentByArticleId(int articleId) {
        if (articleService.hasArticle(articleId)){
            iUnreadCommentImpl.updateUnreadCommentByArticleId(articleId);
        }
    }

    /**
     * 删除某一未读评论
     *
     * @param unreadComment
     * @return
     */
    public ResponseMessage deleteUnreadComment(UnreadComment unreadComment) {
        if (unreadComment.getUserId() > 0
                && unreadComment.getArticleId() > 0){
            if (userService.hasUser(unreadComment.getUserId())){
                if (articleService.hasArticle(unreadComment.getArticleId())){
                    iUnreadCommentImpl.deleteUnreadComment(unreadComment);
                    return responseMessageService.makeMessage(Message.SUCCESS);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }
}
